package user;
import java.util.*;

import edu.cmu.javadb.*;
import edu.cmu.javadb.plan.condition.In;
import static edu.cmu.javadb.plan.condition.Condition.*;

public class Program {
	public static <T>void showAll(Iterable<T> c) {
		for (T rec : c) {
			System.out.print(rec + " ");
			System.out.println();
		}
	}
	
	public static void main(String[] args) {		
		VCollection<Person> c1 = VCollection.getInstance(Person.class);
		
		Person brbr = new Person("brother's brother", 14, 180);
		Person brbr2 = new Person("brother's brother2", 20, 170);
		
		Person br = new Person("brother", 14, 180, brbr);
		
		c1.add( new Person("A", 11, 130, br));
		c1.add( new Person("B", 12, 130, br));
		c1.add( new Person("C", 13, 150, br));
		c1.add( new Person("D", 14, 140, br));
		c1.add( new Person("E", 15, 140, br));
		c1.add( new Person("F", 16, 130, brbr2));
		Person special = new Person("G", 17, 145, brbr);
		c1.add(special);
		c1.add( new Person("H", 19, 135, brbr));
		System.out.println("\ncollection 1");
		showAll(c1);		
		
		View<Person> c2 = c1.filter(greater(path("Age"), 15));
		System.out.println("\ncollection 2");
		showAll(c2);		
		
		View<Person> c3 = c1.filter(and(greater(path("Age"), 15), greater(path("Height"), 130)));
		System.out.println("\ncollection 3");
		showAll(c3);
		
		System.out.println("\n-----------------------------Now we change G's age to 14");
		special.setAge(10);
		System.out.println("\ncollection 1");
		showAll(c1);
		System.out.println("\ncollection 2");
		showAll(c2);
		System.out.println("\ncollection 3");
		showAll(c3);
		
		System.out.println("\n-----------------------------Now we change G's age to 18");
		special.setAge(18);
		System.out.println("\ncollection 1");
		showAll(c1);
		System.out.println("\ncollection 2");
		showAll(c2);
		System.out.println("\ncollection 3");
		showAll(c3);
		
		System.out.println("\n-----------------------------Test for reference");

		//not use this now TODO fix the problem
//		View<Person> c4 = c1.filter(greater(path(""), path("Brother")));
//		showAll(c4);
		
		View<Person> c5 = c1.filter(greater(path("Age"),  path("Brother.Age")));
		showAll(c5);
		
		System.out.println("\n-----------------------------Test for In");
		View<Person> c6 = c1.filter(in(path("Age"), path("Test")));
		showAll(c6);
		
		System.out.println("\nTest for projection");
		View<Integer> c7 = c1.project(path("Age"));
		showAll(c7);
		
		System.out.println("\nTest for join");
		QueryableSet<Person> vc2 = new QueryableSet<Person>();
		vc2.add(new Person("AA", 11, 130, brbr));
		vc2.add(new Person("BB", 13, 135, brbr2));
		
		View<Person> c8 = c1.join(vc2, path("Brother"), path("Brother"));
		showAll(c8);
	}
}
